/*!
 * 文件名称： main.c
 * 描    述： GPIO-按键查询检测实验主函数文件
 * 版本：     2023-12-03, V1.0
*/

/*
* GD32F303东方红开发板V1.0
* 淘宝商城：   https://juwo.taobao.com
* 技术交流：   https://www.gd32bbs.com/ask/
* 视频学习：   https://space.bilibili.com/475462605
* 微信公众号： gd32bbs
* Copyright    苏州聚沃电子科技有限公司
* 版权所有，盗版必究。
*/

#include "gd32f30x.h"
#include <stdio.h>
#include "bsp_led.h"
#include "bsp_key.h"
#include "bsp_uart.h"
#include "driver_public.h"

/*!
* 说明     main函数
* 输入     无
* 返回值   无
*/
int main(void)
{
	  /* 系统延时初始化 */
    driver_init();
    
    /* 按键组初始化 */
    bsp_key_group_init();

	  /* LED组初始化 */
    bsp_led_group_init();
    
	  /* 板载UART初始化 */
    bsp_uart_init(&BOARD_UART);
    
    delay_ms(1000);
    
    printf_log("Example of key scan detection.\r\n");
    
    
	while (1)
	{
        delay_ms(1);
                           
		    /* 按键扫描结果检查：检测任意按键和多按键组合按下时间，所有按键弹起后有效 */
        if(SET==key_scan(1))
        {                           
					  /* 检测按键组合按下时长 */
            if(KEY1.press_timerms >= PRESS_3000MS && KEY2.press_timerms >= PRESS_3000MS && WKUP_KEY.press_timerms >= PRESS_3000MS)
            {
                printf_log("KEY1/KEY2/WKUP_KEY pressed together for more than 3000ms.\r\n");
                KEY1.press_timerms=PRESS_NONE;
                KEY2.press_timerms=PRESS_NONE;
                WKUP_KEY.press_timerms=PRESS_NONE;                  
            }
            else if(KEY1.press_timerms >= PRESS_50MS && KEY2.press_timerms >= PRESS_50MS && WKUP_KEY.press_timerms >= PRESS_50MS)
            {
                printf_log("KEY1/KEY2/WKUP_KEY pressed together for more than 50ms.\r\n");
                KEY1.press_timerms=PRESS_NONE;
                KEY2.press_timerms=PRESS_NONE;
                WKUP_KEY.press_timerms=PRESS_NONE;                  
            }
            
						/* 检测任意按键按下时长 */
            if(ROCKER_KEY.press_timerms >= PRESS_200MS && ROCKER_KEY.press_timerms < PRESS_5000MS)
            {
                ROCKER_KEY.press_timerms=PRESS_NONE;
                printf_log("ROCKER_KEY press more than 200ms, less than 5000ms .\r\n");
            }            
            else if(ROCKER_KEY.press_timerms >= PRESS_5000MS)
            {
                ROCKER_KEY.press_timerms=PRESS_NONE;                
                printf_log("ROCKER_KEY press more than 5000ms.\r\n");
            }            
            else if(ROCKER_KEY.press_timerms >= PRESS_DOWN)
            {
                ROCKER_KEY.press_timerms=PRESS_NONE;                
                printf_log("ROCKER_KEY press briefly.\r\n");
            }  

            if(KEY1.press_timerms >= PRESS_200MS && KEY1.press_timerms < PRESS_5000MS)
            {
                KEY1.press_timerms=PRESS_NONE;                
                printf_log("KEY1 press more than 200ms, less than 5000ms .\r\n");
            }            
            else if(KEY1.press_timerms >= PRESS_5000MS)
            {
                KEY1.press_timerms=PRESS_NONE;                 
                printf_log("KEY1 press more than 5000ms.\r\n");
            }            
            else if(KEY1.press_timerms >= PRESS_DOWN)
            {
                KEY1.press_timerms=PRESS_NONE;                 
                printf_log("KEY1 press briefly.\r\n");
            } 

            if(KEY2.press_timerms >= PRESS_200MS && KEY2.press_timerms < PRESS_5000MS)
            {
                KEY2.press_timerms=PRESS_NONE;                 
                printf_log("KEY2 press more than 200ms, less than 5000ms .\r\n");
            }            
            else if(KEY2.press_timerms >= PRESS_5000MS)
            {
                KEY2.press_timerms=PRESS_NONE;                
                printf_log("KEY2 press more than 5000ms.\r\n");
            }            
            else if(KEY2.press_timerms >= PRESS_DOWN)
            {
                KEY2.press_timerms=PRESS_NONE;                
                printf_log("KEY2 press briefly.\r\n");
            } 

            if(WKUP_KEY.press_timerms >= PRESS_200MS && WKUP_KEY.press_timerms < PRESS_5000MS)
            {
                WKUP_KEY.press_timerms=PRESS_NONE;                
                printf_log("WKUP_KEY press more than 200ms, less than 5000ms .\r\n");
            }            
            else if(WKUP_KEY.press_timerms >= PRESS_5000MS)
            {
                WKUP_KEY.press_timerms=PRESS_NONE;                                
                printf_log("WKUP_KEY press more than 5000ms.\r\n");
            }            
            else if(WKUP_KEY.press_timerms >= PRESS_DOWN)
            {
                WKUP_KEY.press_timerms=PRESS_NONE;                                
                printf_log("WKUP_KEY press briefly.\r\n");
            }                                                           
        }

				/* 按键扫描结果检查：检测任意按键有按下 */
        if(ROCKER_KEY.press_timerms == PRESS_DOWN)
        {              
            ROCKER_KEY.press_timerms=PRESS_NONE;            
            bsp_led_toggle(&LED0);             
        }            
        else if(KEY1.press_timerms == PRESS_DOWN)
        {
            KEY1.press_timerms=PRESS_NONE; 
            bsp_led_toggle(&LED0); 
        }            
        else if(KEY2.press_timerms == PRESS_DOWN)
        {
            KEY2.press_timerms=PRESS_NONE;   
            bsp_led_toggle(&LED0);             
        }
        else if(WKUP_KEY.press_timerms == PRESS_DOWN)
        {
            WKUP_KEY.press_timerms=PRESS_NONE;            
            bsp_led_toggle(&LED0);             
        }
        else
        {
            bsp_led_off(&LED0); 
        }
        
				/* 直接读取按键有按下 */
        if(bsp_key_state_get(&ROCKER_KEY)==SET)
        {              
            bsp_led_toggle(&LED1);             
        }            
        else if(bsp_key_state_get(&KEY1)==SET)
        {
            bsp_led_toggle(&LED1); 
        }            
        else if(bsp_key_state_get(&KEY2)==SET)
        {
            bsp_led_toggle(&LED1);             
        }
        else if(bsp_key_state_get(&WKUP_KEY)==SET)
        {
            bsp_led_toggle(&LED1);             
        }
        else
        {
            bsp_led_off(&LED1); 
        }                
	}
}

